You are tasked with designing a system that adheres to the Liskov Substitution Principle (LSP) from the SOLID principles. The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. The goal is to create a system where derived classes can be used interchangeably with their base class without causing issues.
public class Bird {
public void fly() {
System.out.println("The bird is flying.");
}
}
public class Sparrow extends Bird {
// Additional methods or overrides can be added
}
public class Penguin extends Bird {
@Override
public void fly() {
System.out.println("Penguins cannot fly.");
}
}
public class Main {
public static void main(String[] args) {
Bird sparrow = new Sparrow();
sparrow.fly(); // Outputs: "The bird is flying."
Bird penguin = new Penguin();
penguin.fly(); // Outputs: "Penguins cannot fly."
}
}